package com.ws.client.utils;

import org.openqa.selenium.chrome.ChromeDriver;

public class LocalStorageUtils {

    /**
     * 获取手机号
     */
    public static String getPhoneNumber(ChromeDriver chromeDriver) {
        String phoneNumber = getData(chromeDriver, "last-wid-md");
        return phoneNumber == null ? null : phoneNumber.split(":")[0].replace("\"", "");
    }

    /**
     * 获取对话联系人
     */
    public static String getContact(ChromeDriver chromeDriver) {
        try {
            chromeDriver.executeScript("function test(){let t=indexedDB.open('model-storage');t.onsuccess=function(){const e=t.result.transaction('chat','readonly').objectStore('chat').getAll();let o='';e.onsuccess=(()=>{for(item of e.result)item.id.length>8&&(o+=item.id.split('@')[0]+'|');window.getids=o})}}test();");
            Thread.sleep(500);
            String result = (String)chromeDriver.executeScript("return window.getids");
            LogHelper.debug("读取到联系人:" + result);
            return result;
        }catch (Exception e){
            LogHelper.debug("读取到联系人异常:" + e.getMessage());
            e.printStackTrace();
        }
        return "";

//        //企业版
//        String contactByEnterPrise = getData(chromeDriver, "MB66ES5KdgHJcL8jXB2c6Q==");
//        //个人
//        String contactByUser = getData(chromeDriver, "yF32BrUFFCniKzwMJaKOQw==");
//
//        String str = contactByEnterPrise == null ? contactByUser : contactByEnterPrise;
//        if (str == null) return null;
//        str = str.replace("\"", "").replace("\\", "").replace("{", "").replace("}", "");
//        String[] dats = str.split(",");
//        String result = "";
//        for (String res : dats) {
//            result += "-" + res.split("@")[0];
//        }
//        return result.substring(1);
    }

    /**
     * 获取昵称
     */
    public static String getNickname(ChromeDriver chromeDriver) {
        //企业版
        String nickNameByEnterPrise = getData(chromeDriver, "7mvId07tHT/a2SeaC8VZHg==");
        //个人
        String nickNameByUser = getData(chromeDriver, "aCOePied64R9RJ2R3flyzQ==");
        return nickNameByEnterPrise == null ? nickNameByUser : nickNameByEnterPrise;
    }

    /**
     * 中央处理数据
     */
    public static String getData(ChromeDriver chromeDriver, String name) {
        return (String) chromeDriver.executeScript("return window.localStorage.getItem('" + name + "');");
    }
}
